import time
import numpy as np
import pandas as pd
import requests
from get_iot_data import GetIotData
from function_all import (
    add_windows,
    FFT_spectrogram,
    ButterworthFiltering,
    getXampl,
    hilbert_envelop,
    rms_feature,
)
# def xyz(x_axis, y_axis, label):
#     return {
#         "axisX": round(x_axis, 2),
#         "axisY": round(y_axis, 2),
#         "label": label
#     }
# def xyz2(x_axis, y_axis, step):
#     return {
#         "axisX": round(x_axis, 2),
#         "axisY": round(y_axis, 2),
#         "step": round(step, 2)
#     }
# def xy(x_axis, y_axis):
#     return {
#         "axisX": round(x_axis, 2),
#         "axisY": round(y_axis, 2)
#     }
def getXampl2(x_axis, y_axis, motor, motorAllow):
    return_df = pd.DataFrame()

    arr3 = np.vstack((x_axis, y_axis))
    arr3t = arr3.T
    df = pd.DataFrame(arr3t, columns=['axisX', 'axisY'])
    bq_min = motor / 60 * (1 - motorAllow)
    bq_max = motor / 60 * (1 + motorAllow)
    df_bq1 = df[(df['axisX'] >= bq_min) & (df['axisX'] <= bq_max)]
    success = df_bq1.empty is False
    if success is True:
        df_bq1 = df_bq1.sort_values(['axisY'], ascending=False)
        df_bq1 = df_bq1.reset_index(drop=True)
        bq1 = df_bq1.loc[0, 'axisX']
        bq1_energy = df_bq1.loc[0, 'axisY']
        return_df["1xAmpl"] = [round(bq1_energy, 4)]
        for i in range(2, 11):
            bq_tmp = bq1 * i
            df_bq_tmp = df[df['axisX'] == bq_tmp]
            df_bq_tmp = df_bq_tmp.reset_index(drop=True)
            bq_tmp_energy = df_bq_tmp.loc[0, 'axisY']
            return_df[str(i) + "xAmpl"] = [round(bq_tmp_energy, 4)]
    else:
        bq_min2 = motor / 60 * (1 - motorAllow) * 2
        bq_max2 = motor / 60 * (1 + motorAllow) * 2
        df_bq2 = df[(df['axisX'] >= bq_min2) & (df['axisX'] <= bq_max2)]
        success = df_bq2.empty is False
        if success is True:
            df_bq2 = df_bq2.sort_values(['axisY'], ascending=False)
            df_bq2 = df_bq2.reset_index(drop=True)
            bq2 = df_bq2.loc[0, 'axisX']
            bq2_energy = df_bq2.loc[0, 'axisY']
            bq1 = bq2 / 2
            ratio = x_axis[1] - x_axis[0]
            df_bq1 = df[(df['axisX'] >= bq1 - ratio) & (df['axisX'] <= bq1 + ratio)]
            df_bq1 = df_bq1.sort_values(['axisY'], ascending=False)
            df_bq1 = df_bq1.reset_index(drop=True)
            bq1 = df_bq1.loc[0, 'axisX']
            bq1_energy = df_bq1.loc[0, 'axisY']
            return_df["1xAmpl"] = [round(bq1_energy, 4)]
            return_df["2xAmpl"] = [round(bq2_energy, 4)]
            for i in range(3, 11):
                bq_tmp = bq1 * i
                df_bq_tmp = df[(df['axisX'] >= bq_tmp - ratio) & (df['axisX'] <= bq_tmp + ratio)]
                df_bq_tmp = df_bq_tmp.sort_values(['axisY'], ascending=False)
                df_bq_tmp = df_bq_tmp.reset_index(drop=True)
                bq_tmp_energy = df_bq_tmp.loc[0, 'axisY']
                return_df[str(i) + "xAmpl"] = [round(bq_tmp_energy, 4)]
    return return_df

couplingType = '膜片联轴器'
motor = 1480
motorAllow = 0.02
bpfo = 0.1
bpfi = 0.2
bsf = 0.3
fif = 0.4
motor = float(motor)
direction = 'HVIB'
path = "root.BGTA.BGTAIOAA00.IDL4_MF_GWS_01.clN"
now_time = "2023/12/06 14:42:41.560"
run_status, msg, rst = GetIotData.get_iot_signal(path, now_time)
if run_status == True:
    hz = rst['hz']
    cl = rst['cl']
    values = rst['values']
    rawData = values
    fs = float(hz)
    raw_data = np.array(rawData)
    # 去直流
    data_mean = sum(raw_data) / len(raw_data)
    raw_data -= data_mean
    # windowed_data = add_windows(raw_data)
    # # 时频域转换
    # data = np.asarray(windowed_data)
    data = np.asarray(raw_data)
    x_axis, y_axis = FFT_spectrogram(data, fs)
    # 包络：获取特征频率
    # impulse = ButterworthFiltering()
    # impulse_wave = impulse.high_filtering(cutoff_frequency=5, series=raw_data)
    # filtered_data = hilbert_envelop(impulse_wave)
    # if len(filtered_data) > 0:
    #     impulse_mean = rms_feature(filtered_data)
    #     # 根据轴承型号读取特征频率
    #     frequency_name = ['bpfo', 'bpfi', 'bsf', 'fif']
    #     frequency_list = [float(bpfo), float(bpfi), float(bsf), float(fif)]
    #     fault_frequency_list = []
    #     for i in range(len(frequency_list)):
    #         x = motor * frequency_list[i] / 60
    #         y = filtered_data[int(x)]
    #         fault_frequency_list.append(xyz(x, y, frequency_name[i]))
    #     faultFrequency = fault_frequency_list
    # else:
    #     impulse_mean = 0.8
    # print("calculate envelope...", impulse_mean)
    # hlit_list = []
    # sideband_list = []
    # for i in range(3):
    #     x = (i + 1) * float(bpfi) * motor / 60
    #     hlit_list.append(xyz2(x, y_axis[int(x)], float(fif) * motor / 60))
    #     for j in range(2):
    #         base = (j + 1) * float(fif) * motor / 60
    #         sideband_list.append(xy(x + base, y_axis[int(x + base)]))
    #         sideband_list.append(xy(x - base, y_axis[int(x - base)]))
    # hlit = hlit_list
    # sideband = sideband_list
    # code = 1
    # msg = 'ok'
    # xampl0 = getXampl(x_axis, y_axis, fs, motor)
    xampl = getXampl2(x_axis, y_axis, motor, motorAllow)
    conclusion = ''
    advice = ''
    print(len(conclusion))
    # 不平衡
    imbalance_rate = float(xampl["1xAmpl"] / xampl.sum(axis=1))
    if couplingType != '膜片联轴器':
        if imbalance_rate >= 0.7:
            conclusion += '不平衡'
            advice += '调整转子平衡状态，排除轴系不平衡情况'
        else:
            conclusion += ''
            advice += ''
    else:
        if imbalance_rate >= 0.6:
            conclusion += '不平衡'
            advice += '调整转子平衡状态，排除轴系不平衡情况'
        else:
            conclusion += ''
            advice += ''
    # 不对中
    centering_rate1 = float((xampl["1xAmpl"] + xampl["2xAmpl"]) / xampl.sum(axis=1))
    centering_rate2 = float(xampl["2xAmpl"] / xampl["1xAmpl"])
    if centering_rate1 >= 0.6 or centering_rate2 >= 0.5:
        if len(conclusion) == 0:
            conclusion += '对中不良'
            advice += '联轴器重新找正'
        else:
            conclusion += '；对中不良'
            advice += '；且联轴器重新找正'
    else:
        conclusion += ''
        advice += ''
    print(len(conclusion))

    # 轴承故障
    return_df = pd.DataFrame(
        columns=['name', 'ampl', 'value'])
    for i in range(1,4):
        dict = {}
        dict['name'] = 'bpfi'
        dict['ampl'] = i
        ratio = x_axis[1] - x_axis[0]
        energy_range_1 = int((motor / 60 * bpfi * i * (1 - motorAllow)) / ratio)
        energy_range_2 = int((motor / 60 * bpfi * i * (1 + motorAllow)) / ratio)
        energy = max(y_axis[energy_range_1:energy_range_2], default=0)
        dict['value'] =round(energy, 4)
        new_row = pd.Series(dict)
        return_df = return_df.append(new_row, ignore_index=True)
    for i in range(1,4):
        dict = {}
        dict['name'] = 'bpfo'
        dict['ampl'] = i
        ratio = x_axis[1] - x_axis[0]
        energy_range_1 = int((motor / 60 * bpfo * i * (1 - motorAllow)) / ratio)
        energy_range_2 = int((motor / 60 * bpfo * i * (1 + motorAllow)) / ratio)
        energy = max(y_axis[energy_range_1:energy_range_2], default=0)
        dict['value'] = round(energy, 4)
        new_row = pd.Series(dict)
        return_df = return_df.append(new_row, ignore_index=True)
    for i in range(1,4):
        dict = {}
        dict['name'] = 'bsf'
        dict['ampl'] = i
        ratio = x_axis[1] - x_axis[0]
        energy_range_1 = int((motor / 60 * bsf * i * (1 - motorAllow)) / ratio)
        energy_range_2 = int((motor / 60 * bsf * i * (1 + motorAllow)) / ratio)
        energy = max(y_axis[energy_range_1:energy_range_2], default=0)
        dict['value'] =round(energy, 4)
        new_row = pd.Series(dict)
        return_df = return_df.append(new_row, ignore_index=True)
    for i in range(1,4):
        dict = {}
        dict['name'] = 'fif'
        dict['ampl'] = i
        ratio = x_axis[1] - x_axis[0]
        energy_range_1 = int((motor / 60 * fif * i * (1 - motorAllow)) / ratio)
        energy_range_2 = int((motor / 60 * fif * i * (1 + motorAllow)) / ratio)
        energy = max(y_axis[energy_range_1:energy_range_2], default=0)
        dict['value'] = round(energy, 4)
        new_row = pd.Series(dict)
        return_df = return_df.append(new_row, ignore_index=True)
    sum_12 = return_df['value'].sum()
    if sum_12 > 0.5 * float(xampl["2xAmpl"]):
        if len(conclusion) == 0:
            conclusion += '轴承故障'
            advice += '增加润滑，调整轴承装配或者更换轴承'
        else:
            conclusion += '；轴承故障'
            advice += '；增加润滑，调整轴承装配或者更换轴承'

        return_df1 = return_df[return_df['name']=='bpfi']
        return_df1 = return_df1.reset_index(drop=True)
        sum_1 = return_df1['value'].sum()
        return_df2 = return_df[return_df['name']=='bpfo']
        return_df2 = return_df2.reset_index(drop=True)
        sum_2 = return_df2['value'].sum()
        return_df3 = return_df[return_df['name'] == 'bsf']
        return_df3 = return_df3.reset_index(drop=True)
        sum_3 = return_df1['value'].sum()
        return_df4 = return_df[return_df['name'] == 'fif']
        return_df4 = return_df4.reset_index(drop=True)
        sum_4 = return_df4['value'].sum()
        if sum_1 > 0.4 * sum_12:
            conclusion += '，轴承内圈故障'
        if sum_2 > 0.4 * sum_12:
            conclusion += '，轴承外圈故障'
        if sum_3 > 0.4 * sum_12:
            conclusion += '，轴承滚动体故障'
        if sum_4 > 0.4 * sum_12:
            conclusion += '，轴承保持架故障'
    else:
        conclusion += ''
        advice += ''
    print(len(conclusion))

print('finish')